﻿using OcrAnswerQuestionsForm;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WuXiaOcrAQ
{
    public partial class MessageForm : AntdUI.Window
    {
        public static MessageForm instance;

        public static MessageForm CreateForm()
        {
            if (instance == null || instance.IsDisposed)
            {
                instance = new MessageForm();
            }
            return instance;
        }

        AntdUI.Label labelExt = new AntdUI.Label();
        public MessageForm()
        {
            InitializeComponent();
            //AutoDpi(this);

            this.DoubleBuffered = true;
            SetStyle(
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.UserPaint |
                ControlStyles.ResizeRedraw,
                true);

            labelExt.Shadow = 1;            
            labelExt.ForeColor = Color.Red;    
            labelExt.BackColor = Color.Transparent;
            labelExt.Font = new Font("微软雅黑", 48F, FontStyle.Regular);
            labelExt.Location = new Point(100, 100);
            labelExt.Size = new Size(2000, 150);
            labelExt.Text = "示例文本";

            var config1 = ConfigSettings.ReadSetting("MFontSize");
            var config3 = ConfigSettings.ReadSetting("MFontPointX");
            var config4 = ConfigSettings.ReadSetting("MFontPointY");

            if (int.TryParse(config1, out var iConfig1))
            {
                labelExt.Font = new Font("微软雅黑", iConfig1, FontStyle.Regular);
            }

            if (int.TryParse(config3, out var iConfig3) && int.TryParse(config4, out var iConfig4))
            {
                using (Graphics g = this.CreateGraphics())
                {
                    float dpiX = g.DpiX;
                    float scalingFactor = dpiX / 96.0f;
                    labelExt.Location = new Point((int)(iConfig3 / scalingFactor), (int)(iConfig4 / scalingFactor));
                }
            }

            this.Controls.Add(labelExt);

            string config7 = ConfigSettings.ReadSetting("ShowA");
            //labelExt.Visible = (config7 == true.ToString());
            labelExt.Visible = true;
            labelExt1.Visible = false;
            label3.Visible = false;
        }

        private void Message_Load(object sender, EventArgs e)
        {
            this.BackColor = Color.Fuchsia;
            this.TransparencyKey = Color.Fuchsia;
            this.Location = new Point(0, 0);
            this.TopMost = true;
            this.FormBorderStyle = FormBorderStyle.None;

            var config2 = ConfigSettings.ReadSetting("FFontSize");
            if (int.TryParse(config2, out var iConfig2))
            {
                labelExt1.Font = new Font("微软雅黑", iConfig2, FontStyle.Regular);
            }

            string config8 = ConfigSettings.ReadSetting("ShowB");
            //labelExt1.Visible = (config8 == true.ToString());
            labelExt1.Visible = false;
        }

        private void label1_TextChanged(object sender, EventArgs e)
        {
            foreach (Control item in this.Controls)
            {
                if (item is AntdUI.Label labelExt)
                {
                    labelExt.Text = label1.Text;
                }
            }
            // trim！！
            string noSpacesString = label1.Text.Replace(" ", "");
            labelExt1.Text = noSpacesString;
            labelExt.Text = noSpacesString;
        }
    }
}
